package loginRegister;

import Bean.CheckBean;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 黄忠
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        HttpSession session=request.getSession();
        session.invalidate();
        session=request.getSession();
        String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"), "UTF-8");
        String password = new String(request.getParameter("password").getBytes("ISO-8859-1"), "UTF-8");
        LoginBean loginBean = new LoginBean();
        loginBean.setUserName(userName);
        loginBean.setPassword(password);
        if (loginBean.login()) {
            session.setAttribute("login", loginBean);
            response.sendRedirect("http://localhost:8080/main/main.jsp");
        } else {
            CheckBean.alert("用户名或密码不正确!");
            response.sendRedirect("http://localhost:8080/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        doGet(request, response);
    }     
}
